<?php
class sis_sijaka_sertifikat_model extends MY_Model {
	var $model = 'sis_sijaka_sertifikat';
	
	function GetJSON($as_id_sijaka = '')
	{
		$ls_sql	= "	SELECT	A.ID_SIJAKA, A.NO_SERTIFIKAT, A.JML_DANA, A.ST_AKTIF, TO_CHAR(A.TGL_CLOSE, 'DD/MM/YYYY') TGL_CLOSE 
					FROM	SIS_SIJAKA_SERTIFIKAT A 
							LEFT JOIN SIS_SIJAKA_MASTER B ON A.ID_SIJAKA = B.ID_SIJAKA 
					WHERE	1 = 1 
							".($as_id_sijaka == '' ? '' : " AND A.ID_SIJAKA = '".$as_id_sijaka."' ")." 
					ORDER BY ".(post('sort')=='' ? " A.ST_AKTIF DESC, A.NO_SERTIFIKAT " : post('sort'))." ".(post('order')=='' ? 'ASC' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
	
	function GetDetail($as_id_sijaka)
	{
		
		$ls_sql	= "	SELECT	A.ID_SIJAKA, A.NO_SERTIFIKAT, A.JML_DANA, A.ST_AKTIF, TO_CHAR(A.TGL_CLOSE, 'DD/MM/YYYY') TGL_CLOSE 
					FROM	SIS_SIJAKA_SERTIFIKAT A 
							LEFT JOIN SIS_SIJAKA_MASTER B ON A.ID_SIJAKA = B.ID_SIJAKA 
					WHERE	1 = 1 
							".($as_id_sijaka == '' ? '' : " AND A.ID_SIJAKA = '".$as_id_sijaka."' ")." 
					ORDER BY A.ST_AKTIF DESC, A.NO_SERTIFIKAT ASC";

		$result = $this->db->query($ls_sql);
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return NULL;
		}
	}
	
	function Add($arr_data){
		$this->db->insert('sis_sijaka_sertifikat', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('sis_sijaka_sertifikat', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_id_sijaka, $as_no_sertifikat){
		$result = $this->db->get_where('sis_sijaka_sertifikat', array('id_sijaka' => $as_id_sijaka,  'no_sertifikat' => $as_no_sertifikat));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_id_sijaka, $as_no_sertifikat)
	{
		$result = $this->db->query("SELECT	A.ID_SIJAKA, A.NO_SERTIFIKAT, A.JML_DANA, A.ST_AKTIF, TO_CHAR(A.TGL_CLOSE, 'DD/MM/YYYY') TGL_CLOSE 
									FROM	SIS_SIJAKA_SERTIFIKAT A 
											LEFT JOIN SIS_SIJAKA_MASTER B ON A.ID_SIJAKA = B.ID_SIJAKA 
									WHERE	A.ID_SIJAKA = '".$as_id_sijaka."' AND A.NO_SERTIFIKAT = '".$as_no_sertifikat."'");
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM sis_sijaka_sertifikat WHERE ID_SIJAKA = '".$arr_rule['id_sijaka']."' AND NO_SERTIFIKAT = '".$arr_rule['no_sertifikat']."' AND NVL(ST_AKTIF,'0') <> '0'"))>0)
			{
				return $this->error_delete.' [id: '.$arr_rule['id_sijaka'].", ".$arr_rule['no_sertifikat']."]<br />";
			}else{
				$this->db->delete('sis_sijaka_sertifikat', array('id_sijaka' => $arr_rule['id_sijaka'],  'no_sertifikat' => $arr_rule['no_sertifikat']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>